﻿using System;
using System.Net;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace WindowsPhoneApplication1.Connectors
{
    public class SubscriptionsConnector : Connector
    {
        public static void GetSubscriptionsFrom()
        {
            ThreadPool.QueueUserWorkItem(q =>
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ApiUrl + "subscriptions/from");
                req.Method = "GET";
                req.Headers["X-Blip-API"] = "0.02";
                req.Accept = "application/json";

                AppendAuthHeader(req);

                req.BeginGetResponse(new AsyncCallback(
                    delegate(IAsyncResult asynchronousResult)
                    {
                        HttpStatusCode statusCode;
                        string resultString = "";

                        if (!req.HaveResponse)
                        {
                            throw new WebException();
                        }

                        try
                        {
                            using (HttpWebResponse resp = (HttpWebResponse)((HttpWebRequest)asynchronousResult.AsyncState).EndGetResponse(asynchronousResult))
                            {
                                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                                {
                                    resultString = sr.ReadToEnd();
                                    statusCode = resp.StatusCode;
                                    sr.ReadToEnd();
                                    sr.Close();
                                }
                            }
                        }
                        catch
                        {
                            statusCode = HttpStatusCode.GatewayTimeout;
                        }

                        if (statusCode == HttpStatusCode.OK)
                        {
                            try
                            {
                                List<WebServiceDataSources.Subscription> subscriptions = new List<WebServiceDataSources.Subscription>();
                                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
                                {
                                    var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(subscriptions.GetType());
                                    subscriptions = ser.ReadObject(ms) as List<WebServiceDataSources.Subscription>;

                                    if (Alpp != null)
                                        Alpp.UpdateSubsriptionsList(subscriptions);
                                }
                            }
                            catch (Exception e)
                            {
                                e.ToString();
                                GetSubscriptionsFrom();
                            }
                        }
                    }), req);
            });
        }
    }
}
